package com.dcloud.manage.dblib.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.manage.dblib.entity.DatalibLogicAct;
import com.dcloud.manage.dblib.entity.request.QueryTableActReq;
import com.dcloud.manage.dblib.entity.vo.DatalibLogicActVO;
import com.dcloud.manage.dblib.mapper.DatalibLogicActMapper;
import com.dcloud.manage.dblib.service.IDatalibLogicActService;
import com.dcloud.utils.SpringBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: datalib_logic_act
 * @Author: jeecg-boot
 * @Date: 2024-03-11
 * @Version: V1.0
 */
@Service
public class DatalibLogicActServiceImpl extends ServiceImpl<DatalibLogicActMapper, DatalibLogicAct> implements IDatalibLogicActService {

    @Autowired
    private DatalibLogicActMapper datalibLogicActMapper;

    /**
     * @param tableId
     * @Description:根据表id查询逻辑表字段映射
     * @return: List<DatalibLogicActVO>
     * @Author: shijf
     * @Date 2024/3/14 19:18
     */
    @Override
    public List<DatalibLogicActVO> getListByTableId(String tableId) {


        List<DatalibLogicActVO> list = datalibLogicActMapper.getListByTableId(tableId);
        return list;

    }

    /**
     * @param queryTableActReq
     * @Description:
     * @return: List<DatalibLogicActVO>
     * @Author: shijf
     * @Date 2024/3/27 15:51
     */
    @Override
    public List<DatalibLogicActVO> queryListByReq(QueryTableActReq queryTableActReq) {

        //根据tableId查询所有逻辑表字段映射
        List<DatalibLogicAct> datalibLogicActs = datalibLogicActMapper.selectList(new LambdaQueryWrapper<DatalibLogicAct>().eq(DatalibLogicAct::getDatalibTableId, queryTableActReq.getTableId()));
        List<DatalibLogicActVO> datalibLogicActVOS = SpringBeanUtil.copyProperties(datalibLogicActs, DatalibLogicActVO.class);

        //如果没有加过滤条件，就返回所有字段映射数据
        if (queryTableActReq.getSelectType() == null && queryTableActReq.getSelectType().isEmpty()) {
            return datalibLogicActVOS;
        }

        //根据selectType 过滤数据
        switch (queryTableActReq.getSelectType()) {
            case "dateType":
                //过滤出逻辑表字段映射
                datalibLogicActVOS = datalibLogicActVOS.stream()
                        .filter(entity -> entity.getLogicDataType().equals("DATE")
                                || entity.getLogicDataType().equals("DATETIME")
                                || entity.getLogicDataType().equals("TIMESTAMP")).collect(Collectors.toList());
                break;
            case "noDateType":
                //过滤出逻辑表字段映射
                datalibLogicActVOS = datalibLogicActVOS.stream()
                        .filter(entity -> !entity.getLogicDataType().equals("DATE")
                                && !entity.getLogicDataType().equals("DATETIME")
                                && !entity.getLogicDataType().equals("TIMESTAMP")).collect(Collectors.toList());
                break;
            case "keyType":
                //过滤出逻辑表字段映射
                datalibLogicActVOS = datalibLogicActVOS.stream()
                        .filter(entity -> entity.getIsKey() == 1).collect(Collectors.toList());
                break;
        }


        return datalibLogicActVOS;
    }
}
